﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using PPAP.BO;

namespace PPAP.Web.SearchReport
{
    public partial class SamplingReport : PageBase
    {
        PPARBO pbo = new PPARBO();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                btnSubmit_Click(null, null);
            }
        }


        protected void btnSubmit_Click(object sender, EventArgs e)
        {
            try
            {
                DataSet ds = GetDataSet("");
                AspNetPager2.RecordCount = ds.Tables[0].Rows.Count;
                dgList.DataSource = GetPageTable(ds.Tables[0], AspNetPager2.PageSize, AspNetPager2.CurrentPageIndex);
                dgList.DataBind();
            }
            catch (Exception ex)
            {
                AlertMessage(ex.Message.ToString());
            }
        }

        private DataSet GetDataSet(string sort)
        {
            string sql = " where 1=1 ";
            if (txtPartNo.Text.Trim() != "")
                sql += " and lower(bdntpartno) like lower('%" + CheckWord(txtPartNo.Text.Trim()) + "%') ";
            if (txtSupplierNo.Text.Trim() != "")
                sql += " and lower(suppliercode) like lower('%" + CheckWord(txtSupplierNo.Text.Trim()) + "%') ";
            if (txtCreateStart.Text.Trim() != "")
                sql += " and trunc(endtime)>=to_date( '" + txtCreateStart.Text.Trim() + "', 'yyyy-mm-dd ') ";
            if (txtCreateEnd.Text.Trim() != "")
                sql += " and trunc(endtime)<=to_date( '" + txtCreateEnd.Text.Trim() + "', 'yyyy-mm-dd ') ";
            if (txtSamplingReportNo.Text.Trim() != "")
                sql += " and lower(samplingreportno) like lower('%" + CheckWord(txtSamplingReportNo.Text.Trim()) + "%') ";
            if (ddlStatus.SelectedValue != "All")
            {
                if (ddlStatus.SelectedValue == "5")
                    sql += " and (status=5 or (status=6 and FINALSTATUS=0)) ";
                else
                    sql += " and status=" + ddlStatus.SelectedValue + " ";
            }
            if (ddlReason.SelectedValue != "All")
                sql += " and  reasonforsampling='" + ddlReason.SelectedValue + "' ";
            if (txtVersion.Text.Trim() != "")
                sql += " and lower(bdntrevisionno) like lower('%" + CheckWord(txtVersion.Text.Trim()) + "%') ";
            sql += " and status=7 ";
            sql += sort;
            DataSet ds = pbo.GetReportForSearch(sql);
            return ds;
        }

        public string ShowAuditStatus(string Status, string FINALSTATUS)
        {
            string result = "";
            result = "<span style='color:red'>已提交</span>";

            switch (Status.ToLower())
            {
                case "0":
                    result = "<span style='color:blue'></span>";
                    break;
                case "1":
                    result = "<span style='color:red'>Created</span>";
                    break;
                case "2":
                    result = "<span style='color:red'>Submitted</span>";
                    break;
                case "3":
                    result = "<span style='color:red'>Ready for sampling</span>";
                    break;
                case "4":
                    result = "<span style='color:red'>Not ready for sampling</span>";
                    break;
                case "5":
                    result = "<span style='color:red'>Circulating</span>";
                    break;
                case "6":
                    if (FINALSTATUS == "0")
                        result = "<span style='color:red'>Circulating</span>";
                    else
                        result = "<span style='color:blue'>Released</span>";
                    break;
                case "7":
                    result = "<span style='color:blue'>Decided</span>";
                    break;

                default:
                    result = "<span style='color:red'></span>";
                    break;
            }
            //}
            return result;
        }



        protected void AspNetPager2_PageChanged(object sender, EventArgs e)
        {
            BindGridView();
        }

        /// <summary>
        /// Export
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnExport_Click(object sender, EventArgs e)
        {
            try
            {
                DataSet ds = GetDataSet("");
                foreach (DataRow r in ds.Tables[0].Rows)
                {
                    r["d1"] = r["ppardate"].ToString() == "" ? r["ppardate"] : DateTime.Parse(r["ppardate"].ToString()).ToString("yyyy-MM-dd");
                }
                ds.Tables[0].Columns.Remove("STATUS");
                ds.Tables[0].Columns.Remove("id");
                ds.Tables[0].Columns.Remove("PPARDATE");
                ds.Tables[0].Columns.Remove("FINALSTATUS");
                ds.Tables[0].Columns.Remove("REASONFORSAMPLING");
                ds.Tables[0].Columns.Remove("endtime");
                ds.Tables[0].Columns.Remove("PPARVERSION");
                ds.Tables[0].Columns.Remove("bdntrevisionno");

                ds.Tables[0].Columns["d1"].ColumnName = "Created date";
                ds.Tables[0].Columns["ROWNUM"].ColumnName = "No.";
                ds.Tables[0].Columns["SAMPLINGREPORTNO"].ColumnName = "Sampling report No.";
                ds.Tables[0].Columns["PPARNO"].ColumnName = "PPAR.No";
                ds.Tables[0].Columns["BDNTPARTNO"].ColumnName = "Part.No";
                ds.Tables[0].Columns["SUPPLIERCODE"].ColumnName = "Suppiler Code";
               // ds.Tables[0].Columns["PPARVERSION"].ColumnName = "PPAR version";
                ds.Tables[0].Columns["ST"].ColumnName = "Status";


                CreateExcel(ds, "SampleCard");
            }
            catch (Exception ex)
            {
                AlertMessage(ex.Message.ToString());
            }
        }

        protected void dgList_SortCommand(object source, DataGridSortCommandEventArgs e)
        {
            // 从事件参数获取排序数据列
            string sortExpression = e.SortExpression.ToString();

            // 假定为排序方向为“顺序”
            string sortDirection = "ASC";
            // “ASC”与事件参数获取到的排序方向进行比较，进行GridView排序方向参数的修改
            if (sortExpression == this.dgList.Attributes["SortExpression"])
            {
                //获得下一次的排序状态
                sortDirection = (this.dgList.Attributes["SortDirection"].ToString() == sortDirection ? "DESC" : "ASC");
            }
            // 重新设定GridView排序数据列及排序方向
            this.dgList.Attributes["SortExpression"] = sortExpression;
            this.dgList.Attributes["SortDirection"] = sortDirection;


            string pic = "";
            pic = sortDirection == "ASC" ? "<img src='../Images/icon/arrowup.gif' />" : "<img src='../Images/icon/arrowdown.gif' />";

            foreach (DataGridColumn item in dgList.Columns)
            {
                if (item.SortExpression == sortExpression)
                {
                    item.HeaderText = item.FooterText + " " + pic;
                }
                else
                    item.HeaderText = item.FooterText;
            }

            this.BindGridView();

        }

        private void BindGridView()
        {
            // 获取GridView排序数据列及排序方向
            string sortExpression = this.dgList.Attributes["SortExpression"];
            string sortDirection = this.dgList.Attributes["SortDirection"];
            if (sortExpression != null)
            {
                // 调用业务数据获取方法
                DataTable dtBind = GetDataSet(string.Format("  order by lower({0})  {1} ", sortExpression, sortDirection)).Tables[0];
                // 根据GridView排序数据列及排序方向设置显示的默认数据视图
                if ((!string.IsNullOrEmpty(sortExpression)) && (!string.IsNullOrEmpty(sortDirection)))
                {
                    dtBind.DefaultView.Sort = string.Format("{0} {1}", sortExpression, sortDirection);
                }
                // GridView绑定并显示数据
                this.dgList.DataSource = GetPageTable(dtBind, AspNetPager2.PageSize, AspNetPager2.CurrentPageIndex);
                this.dgList.DataBind();
            }else
                btnSubmit_Click(null, null);
        }
    }
}